'use strict';
const dayjs = require("dayjs");

module.exports = app => {
    const { INTEGER, STRING, DATE, TINYINT } = app.Sequelize;
    const ItemCategory = app.model.define(
        'ItemCategory',
        {
            id: { type: INTEGER, primaryKey: true, autoIncrement: true },
            category_name: { type: STRING(50), allowNull: false, comment: '分类名称' },
            parent_id: { type: INTEGER, defaultValue: 0, comment: '父级ID' },
            sort_order: { type: INTEGER, defaultValue: 0, comment: '排序' },
            status: { type: TINYINT(1), defaultValue: 1, comment: '状态：0-禁用，1-启用' },
            create_time: {
                type: DATE,
                get() {
                    return dayjs(this.getDataValue('create_time')).format('YYYY-MM-DD HH:mm:ss');
                }
            },
            update_time: {
                type: DATE,
                get() {
                    return dayjs(this.getDataValue('update_time')).format('YYYY-MM-DD HH:mm:ss');
                }
            }
        },
        {
            tableName: 'item_category',
            timestamps: true,
            createdAt: 'create_time',
            updatedAt: 'update_time'
        }
    );

    ItemCategory.associate = function() {
        app.model.ItemCategory.hasMany(app.model.Item, { foreignKey: 'category_id', targetKey: 'id' });
    };

    return ItemCategory;
};
